package com.lezhi.socialgram.api.sys;

import com.lezhi.socialgram.model.sys.VeriCode;
import com.lezhi.socialgram.service.sys.SMSVeriService;
import com.lezhi.socialgram.util.Result;
import com.lezhi.socialgram.util.ValidUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by daizhen on 2017/3/23.
 */
@RestController
public class VerifyCodeApi {

    private static final Logger logger = LoggerFactory.getLogger(VerifyCodeApi.class);

    @Autowired
    SMSVeriService smsVeriService;

    @GetMapping("/getVerifyCode/telNum/{telNum}")
    public Result sendSMSVeriCodeByTelNum(@PathVariable("telNum") String telNum) {
        return smsVeriService.getSMSVeriCode(telNum);
    }

    @GetMapping("/getVerifyCode/id/{id}")
    public Result sendSMSVeriCodeByUserId(@PathVariable("id") Long id) {
        return smsVeriService.getSMSVeriCode(id);
    }

    @PostMapping("/verifySMSCode")
    public Result verifySMSCode(@RequestBody @Valid VeriCode veriCode, BindingResult bindingResult) {
        ValidUtils.valid(logger, bindingResult);
        return smsVeriService.verifySMSCode(veriCode.getTelNum(), veriCode.getCode());
    }

}
